﻿/**
 * jQuery plugin 
 */
(function($){
	$.LB = {
		config: {
			SC: '',
			FADE_TIME: 300
		},
		init: function(){
			this.bindEvents();
		},
		bindEvents: function(){			
			//set lightBox Scroll
			var that = this;
			$(window).bind('scroll resize', function(){
				that.lbSetScroll.call(that, null);
			});
			that.move();
		},
		
		// @returns window width	
		calculateScreenWidth: function(){	
			return $(window).width();	
		},		
		// @returns window height		
		calculateScreenHeight: function(){	
			if(document.all){	
				var h = $('body').height();	
				if(h > $(window).height()){	
					return h + 20;
				}else{	
					return $(window).height();	
				}	
			}else{	
				var h = $('html').height();	
				if(h > $(window).height()){	
					return $('html').height();	
				}else{	
					return $(window).height();	
				}	
			}	
		},	
		// @returns the X position that will center an object		
		calculateCenterX: function(obj){
			return $(window).width()/2 - $(obj).width()/2;	
		},	
		// @returns the Y position that will center an object (adjusted to scroll top)		
		calculateCenterY: function(obj){
			return $(window).scrollTop() + ($(window).height()/2 - $(obj).height()/2);	
		},		
		//lightBox scroll
		lbSetScroll: function(){
			var that = this;
			if($('.dialogWrap').is(':visible')){
				clearTimeout(that.config.SC);
				that.config.SC = setTimeout(that.reposition, that.config.FADE_TIME);
			}
		},		
		//reposition
		reposition: function(){
			$('.dialogWrap:visible').each(function(){
				$(this).stop(true, true).animate({
					left: $.LB.calculateCenterX($(this))+'px',
					top: $.LB.calculateCenterY($(this))+'px'
				}, $.LB.config.FADE_TIME);
			});
		},
		move: function(){
			var _move=false,_x,_y,
				self = $('.dialogWrap:visible'),
				selfH = $('.dialogWrap:visible h3'),
				dialogW = self.width(),
				dialogH = self.height(),
				windowW = this.calculateScreenWidth(),
				windowH = $('body').height(),
				maxW = windowW- dialogW-2,
				maxH = windowH- dialogH-2;
			selfH.bind('mousedown', function(e){
				_move=true;
				_x = e.pageX - parseInt(self.css("left"));
				_y = e.pageY - parseInt(self.css("top"));
				self.css({opacity: 0.5});
			});				
			$(document).mousemove(function(e){
				if(_move){						
					var x = e.pageX-_x,
						y = e.pageY-_y;
					_currL = x > maxW ? maxW : (x < 0 ? 0 : x);
					_currT = y > maxH ? maxH : (y < 0 ? 0 : y);
					self.css({top: _currT, left: _currL});
				}
			}).mouseup(function(){
				_move=false;
				self.css({opacity: 1});
		  });
		}
	};
	
})(jQuery);

(function($){
	//show panels in center
	$.fn.showPanelCenter = function(){
		this.each(function(){
			var coorX = ($(window).width() - $(this).width())/2;
			var coorY = ($(window).height() - $(this).height())/2;
			coorX = $(window).scrollLeft() + ((coorX>0)?coorX:0);
			coorY = $(window).scrollTop() + ((coorY>0)?coorY:0);
			$(this).showPanelPosition(coorX, coorY);
		});
		
		return this;
	};
	
	//show panels in position
	$.fn.showPanelPosition = function(x, y){
		this.each(function(){
			$(this)
				.css({
					"left": x,
					"top": y
				})
				.show();
		});
		
		return this;
	};
		
	//preload images
	$.fn.preloadIimg = function(arrUrl, fn){
		this.each(function(i){
			if(!arrUrl[i]){
				arrUrl[i] = this.src;
			}
			var img = new Image();
			img.src = arrUrl[i];
			if (img.complete){
				fn.call(img);
			}
			else{
				img.onload = function(){
					fn.call(img);
					this.onload = null;
				}
			}
		});
		
		return this;
	};
	//textarea max character tips
	$.fn.maxCharTips = function(maxChar, jQ_objTips){
		this.each(function(i){
			$(this).keyup(function(){
				var str = $(this).val();
				var strLength = str.length;
				var charRemain = maxChar - strLength;
				var strTips = '( ' + maxChar + ' characters remaining )';
				if(charRemain<0){
					$(this).val(str.substr(0, maxChar));
					
					strTips = '( 0 characters remaining )';
					jQ_objTips.eq(i).addClass("highlight");
					
					var scrollHeight = $(this)[0].scrollHeight;
					$(this).scrollTop(scrollHeight);
				} else{
					strTips = '( ' + charRemain + ' characters remaining )';
					jQ_objTips.eq(i).removeClass("highlight");
				}
				jQ_objTips.eq(i).text(strTips);
			});			
		});
		
		return this;
	};
	
	//fadeout to remove an element
	$.fn.foRemove = function(during){
		during = during ? during : 600;
		this.each(function(){
			var obj = $(this);
			obj.animate({
				opacity: 0
			}, during, function(){
				obj.remove();
			});
		});
		
		return this;
	};
	
	
	//show loading side
	$.fn.showLoading = function(){
		this.each(function(){
			var selfW = $(this).width(),
				selfH = $(this).height(),
				coorX = selfW/2,
				coorY = selfH/2;							
			$('#loading01').css({'width': selfW, 'height': selfH});
			
			var loadingH = $('#loading01').height()/2,
				loadingW = $('#loading01').width()/2;
				
			coorX = $(this).offset().left + ((coorX>0)?coorX:0) - loadingW;
			coorY = $(this).offset().top + ((coorY>0)?coorY:0) - loadingH;
			$('#loading01').showPanelPosition(coorX, coorY);
		});
		
		return this;
	};
	
	//set default text in textbox
	$.fn.setDefaultText = function(strText){
		strText = strText ? strText : "";
		this.each(function(){
			var self = $(this);
			if(self.val() == ""){
				return true;
			}else{
				self.val(strText).addClass('gray');
			}
			
			self.val(strText)
				.focus(function(){
					if(self.val() == strText) 
						$(this).val('').removeClass('gray');
				})
				.blur(function(){
					if(self.val() == "") 
						self.val(strText).addClass('gray'); 
				});
		});
		return this;
	};

})(jQuery);
